Leaflet is one of the most popular open-source JavaScript libraries for interactive maps
(again)
## Reading layer `g2g22' from data source
## `/Users/duc-q.nguyen/Documents/ddj/HEIGVD/R/shp/g2g22.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 2148 features and 19 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 2485424 ymin: 1075268 xmax: 2833837 ymax: 1295937
## Projected CRS: CH1903+ / LV95
ln_df <- swissdd::get_nationalvotes(
geolevel = "municipality", from_date = "2022-05-15",
to_date = "2022-05-15", language = "FR") %>%
filter(id == 6550) %>%
select(canton_name:mun_name, jaStimmenInProzent, gueltigeStimmen)
ln_df <- left_join(
ln_df %>% mutate(mun_id = as.numeric(mun_id)),
muni %>% select(GMDNR:GRNR),
by = c("mun_id" = "GMDNR")
# To ensure the join returns a sf object
) %>% st_as_sf()# reproject into WGS84 coordinates: https://rspatial.org/raster/spatial/6-crs.html
ln_df <- ln_df %>%
st_transform(crs = 4326) %>%
mutate(
tp = str_c("<h3>", mun_name, "<h3>",
"<i>", canton_name, "</i><br>",
"<b>",
prettyNum(jaStimmenInProzent,
decimal.mark = ",", digits = 3),
"% oui</b>"
) %>% lapply(htmltools::HTML) # important somehow...
)# Function to discretize and colour % yes vote
pal <- colorBin(
"PRGn", domain = ln_df$jaStimmenInProzent ,
bins = seq(0, 100, 10))
lmap <- leaflet(ln_df) %>%
# Define the basemap
addProviderTiles(providers$CartoDB.PositronNoLabels,
options = providerTileOptions(opacity = 1)) %>%
addPolygons(
color = "white", weight = 0.2, smoothFactor = 0.5,
opacity = 0.95, fillOpacity = 0.98,
fillColor = ~pal(jaStimmenInProzent),
label = ln_df$tp
) %>%
# Add basemap labels layer
addMapPane("labels", zIndex = 410) %>% # show labels above
addProviderTiles(providers$CartoDB.PositronOnlyLabels,
options = providerTileOptions(pane = "labels") ) %>%
# Legend box
addLegend(
pal = pal, values = ~jaStimmenInProzent, opacity = 0.7,
position = "bottomleft",
title = "% de oui à Lex Netflix") %>%
# Localized autozoom
addEasyButton(easyButton(
icon="fa-crosshairs", title="Locate Me",
onClick=JS("function(btn, map){ map.locate({setView: true}); }")))Load the cantonal tilemap (“shp/Switzerland_Tiles_CH1903LV03 …”) tilemap for Switzerland
Overlay it over a leaflet basemap. Bonus point if you chose another dark basemap (Basemaps